<?php

namespace app\api\controller\app;

use app\admin\model\AssistantOrder;
use app\admin\model\Banner;
use app\admin\model\City;
use app\admin\model\Org;
use app\admin\model\OrgIndexNotice;
use app\admin\model\OrgNotice;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopdevice;
use app\admin\model\Shopmno;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Tableyue;
use app\admin\model\Userbalance;
use app\admin\model\Userdetail;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userweixin;
use app\api\service\CallService;
use app\api\service\DeviceService;
use app\api\service\MtService;
use app\api\service\MtUtil;
use app\api\service\VbillService;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Http;
use fast\Random;
use think\Config;
use think\Validate;
use function EasyWeChat\Kernel\Support\get_client_ip;

/**
 */
class Active extends Api
{
    protected $noNeedLogin = ['index','getList'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }

    public function index()
    {

        $ret = array();
        $banner = array();
        $banner[]= "https://resouce.51fxyf.com/nbanner1.png";
        $ret['banner'] =$banner;
        $type = array();
        $type[] = "KTV";
        $type[] = "台球";
        $ret['type'] = $type;

        $this->success('',$ret);
    }

    public function getList()
    {

        $page = $this->request->param('page');
        $lat = $this->request->param('lat');
        $lng = $this->request->param('lng');
        $keyword = $this->request->param('keyword');
        $type = $this->request->param('type');
        $activeModel = new \app\admin\model\Active();
        $limit = 10;
        $offset = ($page - 1) * $limit;


        $where = "";
        $keyword = trim($keyword);
        if($keyword != ""){
            $where = " and shop_name like '%".$keyword."%'";
        }
        if($type != ""){
            $where = " and type = '".$type."'";
        }


        $sql = "select * from(
SELECT *,
ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lng*PI()/180-lng*PI()/180)/2),2)))*1000) AS distance
FROM q_active where `status` in ('payed','done') $where) as tmp_table_name
order by distance LIMIT $offset ,$limit";


        $list = $activeModel->query($sql);

        $userDetailModel = new Userdetail();
        $shopModel = new Shop();

        $userModel = new \app\admin\model\User();
        foreach ($list as $key=>$val){
            $list[$key]['create_time_txt'] = date("Y-m-d H:i",$val['create_time']);
            $list[$key]['active_time_txt'] = date("Y-m-d H:i",$val['active_time']);

            $list[$key]['distance'] = round($val['distance']/1000,2)."km";

            $user = $userModel->where('id',$val['user_id'])->find();
            $detail = $userDetailModel->where('user_id',$val['user_id'])->find();
            $list[$key]['avatar'] = cdnurl($user['avatar'],true);
            $list[$key]['username'] = $user['username'];
            $shop = $shopModel->where('id',$val['shop_id'])->find();
            $list[$key]['userDetail'] =$detail;
            $list[$key]['shop'] =$shop;

        }

        $this->success('',$list);
    }




}
